//
//  HOTImagePriviewer.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-15.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTImagePriviewer.h"
#import "MBProgressHUDManager.h"
#import "MBProgressHUD.h"
@interface HOTImagePriviewer () {
    
}
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UIView *mengbanView;;
@property(nonatomic,strong)UIView *view;
@end

@implementation HOTImagePriviewer

+(instancetype)priviewer {
    
    static HOTImagePriviewer *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[[self class] alloc] init];
        instance.imageView = [[UIImageView alloc] init];
        instance.mengbanView = [[UIView alloc] init];
        instance.mengbanView.backgroundColor = [UIColor blackColor];
        
        UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:instance action:@selector(tapMengban)];
        [tgr setNumberOfTapsRequired:1];
        [tgr setNumberOfTouchesRequired:1];
        [instance.mengbanView addGestureRecognizer:tgr];
    });
    
    return instance;
}

-(void)showInView:(UIView *)view withImage:(UIImage *)image {
    
    self.view = view;
    //CGRect cRect = [imageView.superview convertRect:imageView.frame toView:view];
    
    _mengbanView.alpha = 0.0;
    _mengbanView.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    
    CGSize imageSize = image.size;
    float imageW = imageSize.width;
    float imageH = imageSize.height;
    
    if (imageW > 272) {
        //等比例缩放
        imageH = (272.0/imageW)*imageH;
        imageW = 272;
    }
    
    self.imageView.alpha = 0.f;
    self.imageView.image = image;
    self.imageView.frame = CGRectMake((320-imageW)/2, 68, imageW, imageH);
    [self.imageView.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    [self.imageView.layer setBorderWidth:3];
    
    [view addSubview:self.mengbanView];
    [view addSubview:self.imageView];
    [UIView animateWithDuration:0.3 animations:^{
        
        self.mengbanView.alpha = 0.7;
        self.imageView.alpha = 1.f;
        
    }];
}

-(void)showInView:(UIView *)view withImage:(UIImage *)image placeHolder:(UIImage *)pImage {
    
    self.view = view;
    self.imageView.image = pImage;
    
    [self showInView:view withImage:image];
}

-(void)showInView:(UIView *)view withUrl:(NSString *)urlStr placeHolder:(UIImage *)pImage {
    
    self.view = view;
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
    
    [MBProgressHUD showHUDAddedTo:view animated:YES];
    __unsafe_unretained HOTImagePriviewer *tempObj = self;
    [self.imageView setImageWithURLRequest:request placeholderImage:pImage success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideAllHUDsForView:view animated:YES];
            tempObj.imageView.image = image;
            
            CGSize imageSize = image.size;
            float imageW = imageSize.width;
            float imageH = imageSize.height;
            
            if (imageW > 272) {
                //等比例缩放
                imageH = (272.0/imageW)*imageH;
                imageW = 272;
            }
            
            if ((imageH + 68) > view.frame.size.height) {
                
                imageH = view.frame.size.height -10 - 68;
            }
            
            [UIView animateWithDuration:0.3 animations:^{
            
                tempObj.imageView.frame = CGRectMake((320-imageW)/2, 68, imageW, imageH);
            }];
        });
//        tempObj.imageView.image = image;
//        
//        CGSize imageSize = image.size;
//        float imageW = imageSize.width;
//        float imageH = imageSize.height;
//        
//        if (imageW > 272) {
//            //等比例缩放
//            imageH = (272.0/imageW)*imageH;
//            imageW = 272;
//        }
//        
//        //[UIView animateWithDuration:0.3 animations:^{
//            
//            tempObj.imageView.frame = CGRectMake((320-imageW)/2, 68, imageW, imageH);
//        //}];
        
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
        
        
        [MBProgressHUD hideAllHUDsForView:view animated:YES];
    }];
    
    _mengbanView.alpha = 0.0;
    _mengbanView.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);

    CGSize imageSize = pImage.size;
    float imageW = imageSize.width;
    float imageH = imageSize.height;
    
    if (imageW > 272) {
        //等比例缩放
        imageH = (272.0/imageW)*imageH;
        imageW = 272;
    }
    
    if ((imageH + 68) > view.frame.size.height) {
        
        imageH = view.frame.size.height -10 - 68;
    }
    
    self.imageView.frame = CGRectMake((320-imageW)/2, 68, imageW, imageH);
    
    
    self.imageView.alpha = 0.f;
    self.imageView.image = pImage;
    
    [self.imageView.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    [self.imageView.layer setBorderWidth:3];
    
    [view addSubview:self.mengbanView];
    [view addSubview:self.imageView];
    [UIView animateWithDuration:0.3 animations:^{
        
        self.mengbanView.alpha = 0.7;
        self.imageView.alpha = 1.f;
        
    }];
}

-(void)showInWindow {
    
    
}

-(void)show {
    
    
}

-(void)hiddenImagePriviewer {
    
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    [UIView animateWithDuration:0.3 animations:^{
        
        self.mengbanView.alpha = 0.0;
        self.imageView.alpha = 0.0;
        
    } completion:^(BOOL finished) {
        
        if (finished) {
            [self.mengbanView removeFromSuperview];
            [self.imageView removeFromSuperview];
        }
        
    }];
}

-(void)tapMengban {
    
    [self hiddenImagePriviewer];
}

@end
